<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="ctx" value="${pageContext.request.contextPath}"></c:set>
<!DOCTYPE html>
<html lang="en">
<head>
<link href="${ctx}/static/login/css/bott.css" rel="stylesheet"
	type="text/css" />
<link href="${ctx}/static/login/css/style.css" rel="stylesheet"
	type="text/css" /> 
	 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript"
	src="${ctx}/static/js/jquery-1.9.1.min.js"></script>
</head>
<body class="gray-bg">
	<div class="middle-box text-center loginscreen animated fadeInDown">
		<div>
			<div>
				<img src="${ctx}/static/login/svg/profile_small.svg"
					style="width: 35%;" />
			</div>
			<h3>欢迎使用上家运营管理系统</h3>
			<form class="m-t" role="form" action="index.html">
				<div class="form-group">
					<input type="text" id="loginname" name="loginname"
						class="form-control" placeholder="用户名" required="" />
				</div>
				<div class="form-group">
					<input type="password" id="password" name="password"
						class="form-control" placeholder="密码" required="" />
				</div>
				<button type="button" id="login" onclick="severCheck()"
					class="btn btn-primary block full-width m-b">登录</button>
				<!--    <a href="#"><small>忘记密码?</small></a> -->
			</form>
			<div class="m-t">
				<p>建议使用谷歌浏览器</p>
				<P>1366*768及以上分辨率&copy;2011-2017</P>
				<p>Uphome.cn</p>
			</div>
		</div>
	</div>


	<script
		src="${pageContext.request.contextPath}/static/js/bootstrap.min.js"></script>
	<script
		src="${pageContext.request.contextPath}/static/js/jquery-1.7.2.js"></script>
	<script type="text/javascript"
		src="${pageContext.request.contextPath}/static/js/jquery.tips.js"></script>
	<script type="text/javascript"
		src="${pageContext.request.contextPath}/static/js/jquery.cookie.js"></script>
	<script type="text/javascript">
		document.onkeydown = function(e) {
			e = e || window.event;
			if (e.keyCode == 13) {
				severCheck();
				return false;
			}
		}
		function severCheck() {
			if (check()) {
				var loginname = $("#loginname").val();
				var password = $("#password").val();
				$
						.ajax({
							type : "POST",
							url : '${ctx}/login',
							data : {
								userName : loginname,
								password : password,
								tm : new Date().getTime()
							},
							dataType : 'json',
							cache : false,
							error : function(data) {
								var d = eval("(" + data.responseText + ")");
								if (null != d) {
									if (d.error.code == 'common.login.name.or.passwd.wrong') {
										$("#loginname").tips({
											side : 1,
											msg : d.error.message,
											bg : '#FF5080',
											time : 15
										});
										$("#loginname").focus();
									} else if (d.error.code == 'common.login.security.code.wrong') {
										$("#code").tips({
											side : 1,
											msg : d.error.message,
											bg : '#FF5080',
											time : 15
										});
										$("#code").focus();
									} else if (d.error.code == 'common.login.authentication.failed') {
										$("#loginname").tips({
											side : 1,
											msg : d.error.code,
											bg : '#FF5080',
											time : 5
										});
										$("#loginname").focus();
									} else {
										$("#loginname").tips({
											side : 1,
											msg : d.error.message,
											bg : '#FF5080',
											time : 5
										});
										$("#loginname").focus();
									}
								}
							},
							success : function(data) {
								saveCookie();
								window.location.href = "${pageContext.request.contextPath}/index";
							}
						});
			}
		}

		$(document).ready(function() {
			changeCode();
			$("#codeImg").bind("click", changeCode);
		});

		$(document).keyup(function(event) {
			if (event.keyCode == 13) {
				$("#to-recover").trigger("click");
			}
		});

		function genTimestamp() {
			var time = new Date();
			return time.getTime();
		}

		function changeCode() {
			$("#codeImg").attr(
					"src",
					"${pageContext.request.contextPath}/code.do?t="
							+ genTimestamp());
		}

		//客户端校验
		function check() {

			if ($("#loginname").val() == "") {

				$("#loginname").tips({
					side : 2,
					msg : '登录账号不得为空',
					bg : '#AE81FF',
					time : 3
				});

				$("#loginname").focus();
				return false;
			} else {
				$("#loginname").val(jQuery.trim($('#loginname').val()));
			}

			if ($("#password").val() == "") {

				$("#password").tips({
					side : 2,
					msg : '密码不得为空',
					bg : '#AE81FF',
					time : 3
				});

				$("#password").focus();
				return false;
			}
			if ($("#code").val() == "") {

				$("#code").tips({
					side : 1,
					msg : '验证码不得为空',
					bg : '#AE81FF',
					time : 3
				});

				$("#code").focus();
				return false;
			}

			$("#loginbox").tips({
				side : 1,
				msg : '正在登录 , 请稍后 ...',
				bg : '#68B500',
				time : 10
			});

			return true;
		}

		function savePaw() {
			if (!$("#saveid").attr("checked")) {
				$.cookie('loginname', '', {
					expires : -1
				});
				$.cookie('password', '', {
					expires : -1
				});
				$("#loginname").val('');
				$("#password").val('');
			}
		}

		function saveCookie() {
			if ($("#saveid").attr("checked")) {
				$.cookie('loginname', $("#loginname").val(), {
					expires : 7
				});
				$.cookie('password', $("#password").val(), {
					expires : 7
				});
			}
		}
		function quxiao() {
			$("#loginname").val('');
			$("#password").val('');
		}

		jQuery(function() {
			var loginname = $.cookie('loginname');
			var password = $.cookie('password');
			if (typeof (loginname) != "undefined"
					&& typeof (password) != "undefined") {
				$("#loginname").val(loginname);
				$("#password").val(password);
				$("#saveid").attr("checked", true);
				$("#code").focus();
			}
		});
	</script>
	<script>
		//TOCMAT重启之后 点击左侧列表跳转登录首页 
		if (window != top) {
			top.location.href = location.href;
		}
	</script>

</body>
</html>
